﻿namespace GSMock
{
    using System;
    using System.Web;

    /// <summary>
    ///   Handles the search request.
    /// </summary>
    public class SearchRequestHandler : IHttpHandler
    {
        /// <summary>
        ///   You will need to configure this handler in the web.config file of your 
        ///   web and register it with IIS before being able to use it. For more information
        ///   see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            // Google Search Protocol Reference
            // http://code.google.com/apis/searchappliance/documentation/68/xml_reference.html
            if (String.IsNullOrEmpty(context.Request.QueryString.ToString()))
            {
                context.Response.Write("No query parameters - give me nothing, you get nothing back!");
                context.Response.Write("<br />Maybe try using the <a href=\"/GSMockTest.aspx\">test page</a>");
            }
            else
            {
                try
                {
                    ResponseBuilder rb = new ResponseBuilder(context.Request.QueryString);
                    context.Response.Write(rb.GetResponse());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
}
